//
//  HXDPictureTableViewCell.m
//  qingDaoCircleOfLife
//
//  Created by huxuedong on 15/11/24.
//  Copyright © 2015年 huxuedong. All rights reserved.
//

#import "HXDPictureTableViewCell.h"

#define kImageNum 3
#define kScrollViewSize self.scrollView.bounds.size

@interface HXDPictureTableViewCell () <UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property (strong, nonatomic) NSTimer *timer;
@property (strong, nonatomic) NSMutableArray *homeData;

@end

@implementation HXDPictureTableViewCell

+ (instancetype)pictureTableViewCell {
    return [[NSBundle mainBundle] loadNibNamed:@"HXDPictureTableViewCell" owner:nil options:nil].lastObject;
}

- (NSMutableArray *)homeData {
    if (!_homeData) {
        _homeData = [NSMutableArray array];
    }
    return _homeData;
}

- (void)setDataModel:(HXDHomeDataModel *)dataModel {
    _dataModel = dataModel;
    CGFloat imgW = kUIScreenWidth;
    CGFloat imgH = kScrollViewSize.height;
    CGFloat imgY = 0;
    for (int i = 0; i < kImageNum; i++) {
        NSDictionary *homeData = self.dataModel.home.focusList[i];
        [self.homeData addObject:homeData];
        UIImageView *imgView = [[UIImageView alloc] init];
        NSURL *url = [NSURL URLWithString:[homeData objectForKey:@"focusCover"]];
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *image = [UIImage imageWithData:data];
        imgView.image = image;
        CGFloat imgX = i * imgW;
        imgView.frame = CGRectMake(imgX, imgY, imgW, imgH);
        [self.scrollView addSubview:imgView];
    }
    self.scrollView.contentSize = CGSizeMake(imgW * kImageNum, 0);
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.pagingEnabled = YES;
    self.pageControl.numberOfPages = kImageNum;
    [self setTimer];
}

//代理方法，显示当前页数
- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
    CGPoint point = self.scrollView.contentOffset;
    CGFloat scorllViewW = kScrollViewSize.width;
    int page = (point.x + scorllViewW / 2) / scorllViewW;
    self.pageControl.currentPage = page;
}

//开始手动拖拽时停止自动播放
- (void) scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self.timer invalidate];
}

//取消手动拖拽时开始自动播放
- (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [self setTimer];
}

//设置定时器
- (void) setTimer {
    //每隔2秒调用一次chengePicture方法
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(chengePicture) userInfo:nil repeats:YES];
    //调整优先级，将计时器添加到消息循环runLoop中，当其它控件滑动时，scrollView会继续播放动画
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void) chengePicture {
    NSInteger page = self.pageControl.currentPage;
    if (page == kImageNum - 1)
        page = 0;
    else
        page += 1;
    self.pageControl.currentPage = page;
    CGPoint point = CGPointMake(page * kUIScreenWidth, 0);
    [self.scrollView setContentOffset:point animated:YES];
    switch (page) {
        case 0:
            self.titleLabel.text = [self.homeData[0] objectForKey:@"focusTitle"];
            break;
        case 1:
            self.titleLabel.text = [self.homeData[1] objectForKey:@"focusTitle"];
            break;
        case 2:
            self.titleLabel.text = [self.homeData[2] objectForKey:@"focusTitle"];
            break;
        default:
            break;
    }
}

@end
